package com.spring.boot.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spring.boot.demo.mapper.UserMapper;
import com.spring.boot.demo.pojo.PageVo;
import com.spring.boot.demo.pojo.User;
import com.spring.boot.demo.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/11/13 21:25
 * @Description
 */
@RestController
@RequestMapping("/api")
public class TestController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/pageHelper")
    public PageInfo<User> query(Integer pageSize, Integer pageNum) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据库
        List<User> list = userMapper.queryList();
        // 封装查询结果
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @GetMapping("/pageHelper02")
    public PageVo query02(Integer pageSize, Integer pageNum) {
        PageVo pageVo = new PageVo(pageNum, pageSize);
        // 开启分页
        PageUtil.startPage(pageVo);
        // 查询数据库
        List<User> list = userMapper.queryList();
        // 封装查询结果
        return PageUtil.endPage(pageVo, list);
    }

}
